package com.aloda.utils;

import cn.hutool.core.lang.Validator;

import java.util.regex.Pattern;

public class CheckUtils {

    public static final String PATTERN_ACCOUNT = "^[A-Za-z0-9]{6,16}$";

    public static final String PATTERN_PASSWORD = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d\\S]{6,16}$";

    public static final String PATTERN_EMAIL = "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$";

    public static final String PATTERN_PHONE = "^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$";

    public static final String PATTERN_PHONE_CODE = "^\\d{6}$";

    public static final String PATTERN_NICKNAME = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,8}$";

    public static final String PATTERN_CATEGORY_NAME = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,15}$";

    public static final String PATTERN_DESC = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{0,15}$";

    public static final String PATTERN_APP_NAME = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,16}$";

    public static final String PATTERN_APP_DESC = "^.{10,200}$";

    public static final String PATTERN_RESULT_NAME = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,16}$";

    /**
     * 校验账号合法性
     * @param account 账号
     * @return false非法 true合法
     */
    public static boolean checkAccount(String account) {
        // 校验账号
        Pattern accountPattern = Pattern.compile(PATTERN_ACCOUNT);
        return account != null
                && !account.isEmpty()
                && accountPattern.matcher(account).matches();
    }

    /**
     * 校验密码合法性
     * @param password 密码
     * @return false非法 true合法
     */
    public static boolean checkPassword(String password) {
        // 校验密码
        Pattern passwordPattern = Pattern.compile(PATTERN_PASSWORD);
        return password != null
                && !password.isEmpty()
                && passwordPattern.matcher(password).matches();
    }

    /**
     * 校验邮箱合法性
     * @param email 邮箱
     * @return false非法 true合法
     */
    public static boolean checkEmail(String email) {
        // 校验邮箱
        Pattern emailPattern = Pattern.compile(PATTERN_EMAIL);
        return email != null
                && !email.isEmpty()
                && emailPattern.matcher(email).matches()
                && Validator.isEmail(email);
    }

    /**
     * 校验手机号合法性
     * @param phone 手机号
     * @return false非法 true合法
     */
    public static boolean checkPhone(String phone) {
        // 校验邮箱
        Pattern phonePattern = Pattern.compile(PATTERN_PHONE);
        return phone != null
                && !phone.isEmpty()
                && phonePattern.matcher(phone).matches();
    }

    /**
     * 检验短信验证码合法性
     * @param code 短信验证码
     * @return false非法 true合法
     */
    public static boolean checkPhoneCode(String code) {
        Pattern pattern = Pattern.compile(PATTERN_PHONE_CODE);
        return code != null
                && !code.isEmpty()
                && pattern.matcher(code).matches();
    }

    /**
     * 检验昵称合法性
     * @param nickname 昵称
     * @return false非法 true合法
     */
    public static boolean checkNickname(String nickname) {
        // 校验昵称
        Pattern nicknamePattern = Pattern.compile(PATTERN_NICKNAME);
        return nickname != null
                && !nickname.isEmpty()
                && nicknamePattern.matcher(nickname).matches();
    }

    /**
     * 检验个人简介合法性
     * @param desc 个人简介
     * @return false非法 true合法
     */
    public static boolean checkDescription(String desc) {
        // 校验个人简介
        Pattern descPattern = Pattern.compile(PATTERN_DESC);
        return desc != null
                && !desc.isEmpty()
                && descPattern.matcher(desc).matches();
    }

    /**
     * 检验应用名称
     * @param appName
     * @return
     */
    public static boolean checkAppName(String appName) {
        Pattern descPattern = Pattern.compile(PATTERN_APP_NAME);
        return appName != null
                && !appName.isEmpty()
                && descPattern.matcher(appName).matches();
    }

    /**
     * 检验应用描述合法性
     * @param appDesc 应用描述
     * @return false非法 true合法
     */
    public static boolean checkAppDescription(String appDesc) {
        // 校验应用描述
        Pattern descPattern = Pattern.compile(PATTERN_APP_DESC);
        return appDesc != null
                && !appDesc.isEmpty()
                && descPattern.matcher(appDesc).matches();
    }

    // 校验结果名称合法性
    public static boolean checkResultName(String resultName) {
        Pattern resultNamePattern = Pattern.compile(PATTERN_RESULT_NAME);
        return resultName != null && !resultName.isEmpty() && resultNamePattern.matcher(resultName).matches();
    }

    // 校验结果描述合法性
    public static boolean checkResultDesc(String resultDesc) {
        return resultDesc != null && resultDesc.length() <= 100;
    }

    public static boolean checkResultProp(String resultProp) {
        return resultProp != null && resultProp.length() <= 50;
    }

    // 校验结果得分范围合法性
    public static boolean checkResultScoreRange(Integer resultScoreRange) {
        return resultScoreRange != null && resultScoreRange >= 0;
    }

    public static boolean checkCategoryName(String categoryName) {
        // 校验昵称
        return categoryName != null
                && !categoryName.isEmpty()
                && categoryName.length() <= 15;
    }
}
